package com.intretech.umsin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intretech.umsin.system.converter.SysRoleMenuVoEntityConverter;
import com.intretech.umsin.system.entity.SysRoleMenuEntity;
import com.intretech.umsin.system.mapper.SysRoleMenuMapper;
import com.intretech.umsin.system.param.SysRoleMenuParam;
import com.intretech.umsin.system.service.SysRoleMenuService;
import com.intretech.umsin.system.vo.SysRoleMenuVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;



/**
* @version V1.0
* 角色和菜单关联:sys_role_menu 对应商业服务实现.
* @ClassName: SysRoleMenuServiceImpl
* @Description: 角色和菜单关联:sys_role_menu 对应商业服务实现.
* @author: 李启联
* @date:   2022-8-6 14:37:22
* @Copyright: Copyright (c) 2022
*/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenuEntity> implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuVoEntityConverter sysRoleMenuVoEntityConverter;


    /**
    * 根据ID查询单个角色和菜单关联对象
    * @param id 组键ID
    * @return 单个角色和菜单关联VO对象
    */
    public SysRoleMenuVo queryById(Serializable id){
        if (id == null) {return null;}
        return sysRoleMenuVoEntityConverter.reverseFromTarget(this.getById(id));
    }


    /**
    * 根据ID集合查询返回角色和菜单关联列表
    * @param idList 组键ID集合
    * @return 角色和菜单关联列表
    */
    public List<SysRoleMenuVo> queryByIds(Collection<? extends Serializable> idList){
        if(CollectionUtils.isEmpty(idList)){return null;}
        return sysRoleMenuVoEntityConverter.reverseFromTarget(this.listByIds(idList));
    }


    /**
    * 分页查询 角色和菜单关联 信息
    * @param page 分页对象
    * @param param 查询参数
    * @return IPage<SysRoleMenuVo> 分页对象
    */
    @Override
    public IPage<SysRoleMenuVo> queryForPage(IPage page,SysRoleMenuParam param){
      IPage<SysRoleMenuEntity> page0 = this.page(page,createQueryWrapper(param));
      IPage<SysRoleMenuVo> pageResult = page0.convert(e -> sysRoleMenuVoEntityConverter.reverseFromTarget(e));
      return pageResult;
    }


    /**
    * 根据条件查询返回 角色和菜单关联 对象列表
    * @param  param 查询参数
    * @return 角色和菜单关联 对象列表
    */
    @Override
    public List<SysRoleMenuVo> queryForList(SysRoleMenuParam param) {
        List<SysRoleMenuEntity> list = this.list(createQueryWrapper(param));
        return sysRoleMenuVoEntityConverter.reverseFromTarget(list);
    }

    /**
    * 构建查询条件
    * @param param 查询参数实体对象
    * @return 构建后的QueryWrapper查询条件
    */
    private QueryWrapper<SysRoleMenuEntity> createQueryWrapper(SysRoleMenuParam param){
        QueryWrapper<SysRoleMenuEntity> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(param)) { return queryWrapper; }
        Optional<SysRoleMenuParam> op = Optional.of(param);
        queryWrapper.lambda()
                .eq(op.map(SysRoleMenuParam::getId).isPresent(),SysRoleMenuEntity::getId,param.getId())
                .eq(op.map(SysRoleMenuParam::getRoleId).isPresent(),SysRoleMenuEntity::getRoleId,param.getRoleId())
                .eq(op.map(SysRoleMenuParam::getMenuId).isPresent(),SysRoleMenuEntity::getMenuId,param.getMenuId())
                .eq(op.map(SysRoleMenuParam::getUpdater).filter(StringUtils::isNotBlank).isPresent(),SysRoleMenuEntity::getUpdater,param.getUpdater())
                .eq(op.map(SysRoleMenuParam::getCreateDate).isPresent(),SysRoleMenuEntity::getCreateDate,param.getCreateDate())
                .eq(op.map(SysRoleMenuParam::getUpdateDate).isPresent(),SysRoleMenuEntity::getUpdateDate,param.getUpdateDate())
        ;
        return queryWrapper;
    }

    /**
    * 添加角色和菜单关联到数据库
    * @param vo  角色和菜单关联 VO对象
    * @return 添加后的角色和菜单关联 VO对象
    */
    @Override
    public SysRoleMenuVo add(SysRoleMenuVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        SysRoleMenuEntity entity = sysRoleMenuVoEntityConverter.convertToTarget(vo);
        if(this.save(entity)){
            return sysRoleMenuVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("添加 角色和菜单关联 单个对象失败!");
            //return null;
        }
    }

    /**
    * 添加角色和菜单关联集合列表到数据库
    * @param vos  角色和菜单关联 VO对象列表
    * @return 添加后的角色和菜单关联 VO对象集合列表
    */
    @Override
    public List<SysRoleMenuVo> adds(List<SysRoleMenuVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<SysRoleMenuEntity> entities = sysRoleMenuVoEntityConverter.convertToTarget(vos);
        if(this.saveBatch(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return sysRoleMenuVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
           throw new RuntimeException("添加 角色和菜单关联 集合列表失败!");
           //return null;
        }
    }

    /**
    * 更新角色和菜单关联到数据库
    * @param vo 要更新的角色和菜单关联 VO对象
    * @return 更新后的角色和菜单关联 VO对象
    */
    @Override
    public SysRoleMenuVo modify(SysRoleMenuVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        SysRoleMenuEntity entity = sysRoleMenuVoEntityConverter.convertToTarget(vo);
        if(this.updateById(entity)){
            return sysRoleMenuVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("更新角色和菜单关联单个对象失败!");
            //return null;
        }
    }

    /**
    * 更新角色和菜单关联集合列表到数据库
    * @param vos 要更新的角色和菜单关联 VO对象集合
    * @return 更新后的角色和菜单关联 VO对象集合
    */
    @Override
    public List<SysRoleMenuVo> modifys(List<SysRoleMenuVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<SysRoleMenuEntity> entities = sysRoleMenuVoEntityConverter.convertToTarget(vos);
        if(this.updateBatchById(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return sysRoleMenuVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
            throw new RuntimeException("更新角色和菜单关联集合列表失败!");
            //return null;
        }
    }	

}

